package ute.tlcn.struts2hibernate.dao;

import java.util.List;

import org.hibernate.Session;

import ute.tlcn.struts2hibernate.javabean.Order;

public class OrderDAOImpl implements OrderDAO {
	
	private Session session;
	
	public OrderDAOImpl() {
		session = HibernateUtil.getSesisonFactory().getCurrentSession();
	}

	@Override
	public void insert(Order order) throws Exception {
		session.persist(order);
	}

	@Override
	public void delete(Integer orderId) throws Exception {
		session.delete(session.load(Order.class, orderId));
	}

	@Override
	public void update(Order order) throws Exception {
		session.update(order);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Order> list() throws Exception {
		return session.createQuery("from Order").list();
	}

	@Override
	public Order findById(Integer orderId) throws Exception {
		return (Order) session.load(Order.class, orderId);
	}
	
	public static void main(String[] args) throws Exception {
		HibernateUtil.getSesisonFactory().getCurrentSession().beginTransaction();
		OrderDAO dao = new OrderDAOImpl();
		Order o = dao.findById(1);
		System.out.println(o.getCustomer().getName());
		HibernateUtil.getSesisonFactory().getCurrentSession().getTransaction().commit();
		HibernateUtil.close();
	}
}
